{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import json\n",
    "file_path = r\"E:\\workspace\\parse_bitstream\\parse_bitstream\\压缩位流相关知识\\MCF100T.json\"\n",
    "# 读取JSON文件\n",
    "with open(file_path, 'r') as file:\n",
    "    data = json.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'FAR': {'frame_type': 0, 'region_type': 0, 'row_num': 0, 'col': 0}, 'frame_count': 42}\n"
     ]
    }
   ],
   "source": [
    "print(data[\"frame_type_0\"][\"region_type_0\"][\"row_num_0\"][\"col_num_0\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 做一个\n",
    "\n",
    "for frame_type_key, frame_type_value in data.items():\n",
    "    # 遍历 frame_type 层\n",
    "    for region_type_key, region_type_value in frame_type_value.items():\n",
    "        # 遍历 region_type 层\n",
    "        for row_num_key, row_num_value in region_type_value.items():\n",
    "            # 遍历 row_num 层\n",
    "            for col_num_key, col_num_value in row_num_value.items():\n",
    "                frame_type = int(re.search(r'\\d+$', frame_type_key).group())\n",
    "                region_type = int(re.search(r'\\d+$', region_type_key).group())\n",
    "                row_num = int(re.search(r'\\d+$', row_num_key).group()) \n",
    "                col_num = int(re.search(r'\\d+$', col_num_key).group()) \n",
    "                FAR = {\n",
    "                    \"frame_type\":frame_type,\n",
    "                    \"region_type\":region_type,\n",
    "                    \"row_num\":row_num,\n",
    "                    \"col_num\":col_num\n",
    "                }\n",
    "                data[frame_type_key][region_type_key][row_num_key][col_num_key][\"FAR\"] = FAR\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = r\"E:\\workspace\\parse_bitstream\\parse_bitstream\\压缩位流相关知识\\MCF100T_v2.json\"\n",
    "with open(filename, 'w') as j: \n",
    "        json.dump(data, j)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "eda_38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
